package com.glorystone.ins.controller;



import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.glorystone.ins.common.common.DataTable;
import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.domain.Attachment;
import com.glorystone.ins.domain.CapitalRecord;
import com.glorystone.ins.domain.Channel;
import com.glorystone.ins.domain.Insurer;
import com.glorystone.ins.domain.SysUser;
import com.glorystone.ins.enums.OpponentEnum;
import com.glorystone.ins.enums.TradeTypeEnums;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.AttachmentService;
import com.glorystone.ins.service.CapitalRecordService;
import com.glorystone.ins.service.ChannelService;
import com.glorystone.ins.service.InsuranceService;
import com.glorystone.ins.service.InsurerService;
import com.glorystone.ins.vo.OrderQueryConditionVo;
import com.glorystone.ins.vo.OrderVo;

/**
 * Created by chenshiyu on 2018/3/20.
 * @author chenshiyu
 */
@Controller
@RequestMapping(value="/insurance")
public class InsuranceController extends BaseController{

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(InsuranceController.class);
	
	@Autowired
	private InsuranceService insuranceService;
	@Autowired
	private ChannelService channelService;
	@Autowired
	private AttachmentService attachmentService;
	@Autowired
	private CapitalRecordService capitalRecordService;
	@Autowired
	private InsurerService insurerService;
	
	@RequestMapping("/toInsuranceList")
	public ModelAndView selectInsuranceList(ModelAndView mv){
		List<Channel> list = channelService.findAll();
		List<Insurer> insurerList = insurerService.selectAllInsurer();
		mv.addObject("list", list);
		mv.addObject("insurerList", insurerList);
		mv.setViewName("insurance/insuranceList");
		return mv;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/queryTableData")
	@ResponseBody
	public DataTable<OrderVo> queryTableData(HttpServletRequest request){
		PageVo vo = parametToPageVo(request,OrderVo.class);
		PageList<OrderVo> list = insuranceService.selectInsuranceList(vo);
		DataTable data = resultToDataTable(list);
		log.info("-------返回结果data------"+data);
		return data;
	}
	
	
	@RequestMapping(value="/toDetail")
	public ModelAndView toDetail(ModelAndView mv,String ordCode,Long tid,Long userId){
		log.info("-------传入参数tid------"+tid+",------传入参数userId------"+userId+",------传入参数ordCode------"+ordCode);
		OrderVo order2 = insuranceService.findOrderByCode(ordCode);
		OrderQueryConditionVo order = new OrderQueryConditionVo();
		order.setTid(tid);
		/**
         * 根据订单Id查询保险资金流水信息
         */
        List<CapitalRecord> recordList = capitalRecordService.selectOrderCapitalRecord(order);
        if (recordList != null) {
            for (CapitalRecord capitalRecord : recordList) {
                if (TradeTypeEnums.UNSURE.getValue() == capitalRecord.getTradeType()) {
                	mv.addObject("unsureRecord", capitalRecord);
                    continue;
                }
                if (OpponentEnum.CHANNEL.getValue() == capitalRecord.getOpponent()) {
                	mv.addObject("companyRecord", capitalRecord);
                } else if (OpponentEnum.INSURANCECOMPANY.getValue() == capitalRecord.getOpponent()) {
                	mv.addObject("channelRecord", capitalRecord);
                }
            }
        }
		List<Attachment> attachmentList = attachmentService.queryAttachList(userId,1);
		mv.addObject("order", order2);
		mv.addObject("attachmentList", attachmentList);
		mv.setViewName("insurance/insuranceDetail");
		return mv;
	}
	
}
